﻿using CommonServiceLocator;
using MassTransit;
using MassTransitTest.Events;
using MassTransitTest.Request;
using Microsoft.AspNetCore.Mvc;
using TC.MQ.RabbitMQ;

namespace MassTransitTest.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class HomeController : ControllerBase
    {
        /// <summary>
        /// Request/Response模式下的事件发送者
        /// </summary>
        private readonly IRequestClient<LoginSuccessRequest> client;

        /// <summary>
        /// 生产消费模式下的消息发送者
        /// </summary>
        private readonly IEventPublisher eventPublisher;

        public HomeController(IRequestClient<LoginSuccessRequest> client, IEventPublisher eventPublisher)
        {
            this.client = client;
            this.eventPublisher = eventPublisher;
        }

        [HttpPost(Name ="Login")]
        public bool Login([FromBody] LoginRequest request)
        {
            var success= client.GetResponse<LoginSuccessResponse>(new LoginSuccessRequest { UserId="222"}).Result.Message.CouponSended;
            if (success)
            {
                eventPublisher.Publish(new OrderSuccessEvent { OrderId = "111" });
            }
            return success;
        }

    }

    public class LoginRequest
    {
        public string UserName { get; set; }
        public string Password { get; set; }
    }
}
